package com.xiaomai.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaomai.commonutil.R;
import com.xiaomai.commonutil.ordervo.CourseOrderVo;
import com.xiaomai.eduservice.entity.EduCourse;
import com.xiaomai.eduservice.entity.chapter.ChapterVo;
import com.xiaomai.eduservice.entity.frontVo.CourseFrontVo;
import com.xiaomai.eduservice.entity.frontVo.CourseWebVo;
import com.xiaomai.eduservice.service.EduChapterService;
import com.xiaomai.eduservice.service.EduCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/courseFront")

public class CourseFrontController {
    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduChapterService chapterService;

    //条件查询带分页查询课程
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable Long page, @PathVariable Long limit,
                                @RequestBody(required = false) CourseFrontVo courseVo){
        //创建分页对象
        Page<EduCourse> pageCourse = new Page<>(page,limit);
        Map<String,Object> map  = courseService.getFrontCourseList(pageCourse,courseVo);
        return R.ok().data(map);
    }
    //课程详情查询
    @GetMapping("getFrontCourseInfo/{courseId}")
    private R getFrontCourseInfo(@PathVariable String courseId){
      CourseWebVo courseWebVo =  courseService.getBaseCourseInfo(courseId);
        List<ChapterVo> chapterVoList = chapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("courseWebVo",courseWebVo).data("chapterVoList",chapterVoList);

    }
    //根据课程id查询课程信息
    @PostMapping("getOrderCourseInfo/{id}")
    public CourseOrderVo getOrderCourseInfo(@PathVariable String id){
        CourseOrderVo courseOrderVo = new CourseOrderVo();
        CourseWebVo baseCourseInfo = courseService.getBaseCourseInfo(id);
        BeanUtils.copyProperties(baseCourseInfo,courseOrderVo);
        return courseOrderVo;
    }

}
